# -*- coding: utf-8 -*-
from odoo import models, fields, api, tools, _
from odoo.exceptions import ValidationError, Warning
from datetime import datetime
from . import config_item

# CHECK_ITEM_TYPE = [
#     ("half_month", "半月维保"),
#     ("quarter", "季度维保"),
#     ("half_year", "半年维保"),
#     ("year", "年度维保")
# ]
# CHECK_ITEM_VALUE = {
#     "half_month" : 15,
#     "quarter": 90,
#     "half_year": 180,
#     "year": 360
# }

class LiftBigCategoryPart(models.Model):
    """
    电梯大类部位 分部位维保用
    """
    _name = "tyibs.main.lift.big.category.part"

    big_category_id = fields.Many2one("tyibs.base.lift.big.category", required=True, string=u"所属大类")
    name = fields.Char(string=u"部位名称", size=100, required=True)
    check_item_ids = fields.One2many("tyibs.main.standard.check.item", "part_id", string=u"检查项")
    remark = fields.Text(string=u"备注")


class LiftBigCategory(models.Model):
    """
    对电梯大类进行扩展
    """
    _inherit = "tyibs.base.lift.big.category"

    part_ids = fields.One2many("tyibs.main.lift.big.category.part", "big_category_id", string=u"电梯部位")
    check_item_ids = fields.One2many("tyibs.main.standard.check.item", compute="_compute_check_items", string=u"检查项")
    min_main_interval = fields.Integer(string=u"最小维保间隔(分)")

    @api.multi
    @api.depends("part_ids")
    def _compute_check_items(self):
        check_items = None
        for item in self:
            for part in item.part_ids:
                if check_items is None:
                    check_items = part.check_item_ids
                else:
                    check_items = check_items | part.check_item_ids
            self.check_item_ids = check_items

            
class StandardCheckItem(models.Model):
    """
    国家标准检查项:
    和自定义检查项合并
    """
    _name = "tyibs.main.standard.check.item"
    _rec_name = "content"
    _order_by = "require_type desc"

    lift_big_category_id = fields.Many2one("tyibs.base.lift.big.category", string=u"所属大类", required=True)
    check_item_type = fields.Selection(config_item.CHECK_ITEM_TYPE, string=u"维保类型", required=True)
    part_id = fields.Many2one("tyibs.main.lift.big.category.part", string=u"所属部位", required=True,
                              domain="[('big_category_id', '=', lift_big_category_id)]")
    scope_type = fields.Selection(config_item.CHECK_ITEM_SCOPE_TYPE, string="要求类型", required=True, default="0")
    enterprise_id = fields.Many2one("tyibs.base.maintenance.company", string=u"所属维保公司", default=lambda self: self.env.user.main_company_id.id)
    content = fields.Char(string=u"保养项目")
    require = fields.Char(string=u"基本要求")
    remark = fields.Text(string=u"备注")


class CheckTemplate(models.Model):
    """
    维保检查模板
    """
    _name = "tyibs.main.check.template"

    # name = fields.Char(string="模板名称", required=True)
    lift_big_category_id = fields.Many2one("tyibs.base.lift.big.category", string=u"所属大类", required=True)
    check_item_type = fields.Selection(config_item.CHECK_ITEM_TYPE, string=u"维保类型", required=True)
    check_item_ids = fields.Many2many("tyibs.main.standard.check.item", "template_id", "check_item_id", string="检查项",
                                      domain="[('lift_big_category_id','=',lift_big_category_id), \
                                               ('check_item_type','=',check_item_type), \
                                               ('scope_type','!=','2')]")
    remark = fields.Text(string=u"备注")

    @api.multi
    @api.depends('lift_big_category_id', 'check_item_type')
    def name_get(self):
        res = []
        check_item_values = dict(self.fields_get("check_item_type")["check_item_type"]["selection"])
        print check_item_values
        for item in self:
            if item.lift_big_category_id and item.check_item_type:
                name = "%s - %s" % (item.lift_big_category_id.name, check_item_values[item.check_item_type])
                res += [(item.id, name)]
        return res


class PayMethod(models.Model):
    """
    支付方式
    """
    _name = "tyibs.main.pay.method"

    name = fields.Char(string=u"付费方式", size=20, required=True)
    cycle = fields.Char(string=u"付费周期(天)", size=20, required=True)
    enterprise_id = fields.Many2one("tyibs.base.maintenance.company", string=u"所属维保公司", default=lambda self: self.env.user.main_company_id.id)
    remark = fields.Text(string=u"备注")


class FreeScope(models.Model):
    """
    免费范围管理
    """
    _name = "tyibs.main.free.scope"

    name = fields.Char(string=u"名称", size=20, required=True)
    detail = fields.Char(string=u"详细", size=20, required=True)
    enterprise_id = fields.Many2one("tyibs.base.maintenance.company", string=u"所属维保公司", default=lambda self: self.env.user.main_company_id.id)
    remark = fields.Text(string=u"备注")


class MaintenanceCycle(models.Model):
    """
    维保周期(最小维保间隔天数)
    """
    _name = "tyibs.main.maintenance.cycle"
    _rec_name = "cycle"

    cycle = fields.Integer(string=u"维保周期", default=15, required=True)
